package com.chengliang.bbs.service.impl;

import com.chengliang.bbs.entry.Collection;
import com.chengliang.bbs.mapper.CollectionMapper;
import com.chengliang.bbs.service.CollectionService;
import com.chengliang.bbs.util.StringUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author chengliang
 */
@Service
public class CollectionServiceImpl implements CollectionService {

    private final CollectionMapper collectionMapper;

    @Autowired
    public CollectionServiceImpl(CollectionMapper collectionMapper) {
        this.collectionMapper = collectionMapper;
    }

    @Override
    public boolean isCollection(Integer userId, Integer jieId) {
        return collectionMapper.selectIsCollectionBy(jieId, userId);
    }

    @Override
    public Page<Collection> getMyCollection(Integer userId, Integer nowPage, Integer pageSize) {
        if(StringUtil.isEmpty(userId)){
            return null;
        }
        PageHelper.startPage(nowPage,pageSize);
        return (Page<Collection>) collectionMapper.selectCollectionListBy(userId);
    }

    @Override
    public boolean addCollection(Integer jieId, Integer userId) {
        if(collectionMapper.selectIsCollectionBy(jieId, userId)){
            collectionMapper.updateCollectionDelete(jieId,userId,false);
        }
        return collectionMapper.insertCollection(jieId,userId);
    }

    @Override
    public boolean removeCollection(Integer jieId, Integer userId) {
        return collectionMapper.updateCollectionDelete(jieId,userId,true);
    }
}
